package com.beichuan_travel.service.impl;

import com.beichuan_travel.enums.ResultCode;
import com.beichuan_travel.enums.Sort;
import com.beichuan_travel.exception.ApiException;
import com.beichuan_travel.interfaces.transform.FoodCovertBasic;
import com.beichuan_travel.interfaces.transform.RestaurantCovertBasic;
import com.beichuan_travel.mapper.RestaurantMapper;
import com.beichuan_travel.model.dto.RestaurantDTO;
import com.beichuan_travel.model.po.FoodPO;
import com.beichuan_travel.model.po.RestaurantPO;
import com.beichuan_travel.model.vo.FoodVO;
import com.beichuan_travel.model.vo.RestaurantVO;
import com.beichuan_travel.service.RestaurantService;
import com.beichuan_travel.utils.ArraysUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("restaurantServiceImpl")
public class RestaurantServiceImpl implements RestaurantService {

    @Resource
    RestaurantMapper restaurantMapper;

    @Resource
    ArraysUtils arraysUtils;


    @Override
    public RestaurantVO queryByName(String name) {
        RestaurantPO restaurantPO = restaurantMapper.queryByName(name);
        if (restaurantPO == null) {
            return null;
        }
        RestaurantVO restaurantVO = new RestaurantVO();
        restaurantVO.setId(restaurantPO.getId());
        restaurantVO.setImgUrl(restaurantMapper.imgUrl(restaurantPO.getId()));
        restaurantVO.setName(restaurantPO.getName());
        restaurantVO.setLocation(restaurantPO.getLocation());
        restaurantVO.setPrice(restaurantPO.getPrice());
        restaurantVO.setDescription(restaurantPO.getDescription());

        return restaurantVO;
    }

    @Override
    public List<RestaurantVO> queryByPrice(Integer lower, Integer upper) {
        List<RestaurantPO> list = restaurantMapper.queryByPrice(lower, upper);
        if (list == null || list.isEmpty()) {
            return null;
        }
        List<RestaurantVO> restaurantVOList = RestaurantCovertBasic.INSTANCE.POtoCoertVO(list);
        return restaurantVOList;
    }

    @Override
    public List<RestaurantVO> queryByStar(Integer star) {
        List<RestaurantPO> list = restaurantMapper.queryByStar(star, star + 1);
        if (list == null || list.isEmpty()) {
            return null;
        }
        List<RestaurantVO> restaurantVOList = RestaurantCovertBasic.INSTANCE.POtoCoertVO(list);
        return restaurantVOList;
    }

    @Override
    public List<RestaurantVO> queryAllDescByStar(Integer type) {
        List<RestaurantPO> list = restaurantMapper.queryAllByStarDesc();
        if (list == null || list.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.ASC.getSort()) {
            arraysUtils.reverse(list);
        }
        List<RestaurantVO> restaurantVOList = RestaurantCovertBasic.INSTANCE.POtoCoertVO(list);
        return restaurantVOList;
    }

    @Override
    public List<RestaurantVO> queryAllASCByPrice(Integer type) {
        List<RestaurantPO> list = restaurantMapper.queryAllByPriceAsc();
        if (list == null || list.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.DESC.getSort()) {
            arraysUtils.reverse(list);
        }
        List<RestaurantVO> restaurantVOList = RestaurantCovertBasic.INSTANCE.POtoCoertVO(list);
        return restaurantVOList;
    }

    @Override
    public List<RestaurantVO> fuzzyQueryName(String name) {
        List<RestaurantPO> restaurantPOList = restaurantMapper.queryByLikeName("%"+name+"%");
        if (restaurantPOList == null || restaurantPOList.isEmpty()) {
            return null;
        }
        List<RestaurantVO> restaurantVOList = RestaurantCovertBasic.INSTANCE.POtoCoertVO(restaurantPOList);
        return restaurantVOList;
    }

    @Override
    public List<RestaurantVO> queryAllByName(Integer type) {
        List<RestaurantPO> restaurantPOList = restaurantMapper.queryAllByNameAsc();
        if (restaurantPOList == null || restaurantPOList.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.DESC.getSort()) {
            arraysUtils.reverse(restaurantPOList);
        }
        List<RestaurantVO> restaurantVOList = RestaurantCovertBasic.INSTANCE.POtoCoertVO(restaurantPOList);
        return restaurantVOList;
    }

    @Override
    public int addRestaurant(RestaurantDTO restaurantDTO) {
        RestaurantPO restaurantPO = new RestaurantPO();
        restaurantPO.setName(restaurantDTO.getName());
        restaurantPO.setLocation(restaurantDTO.getLocation());
        restaurantPO.setPrice(restaurantDTO.getPrice());
        restaurantPO.setDescription(restaurantDTO.getDescription());

        return restaurantMapper.addRestaurant(restaurantPO);
    }

    @Override
    public List<FoodVO> queryAllFood(Integer restaurantId) {
        List<FoodPO> poList = restaurantMapper.queryAllFood(restaurantId);
        if (poList == null || poList.isEmpty()) {
            return null;
        }
        List<FoodVO> voList = FoodCovertBasic.INSTANCE.POtoCoertVO(poList);
        for (FoodVO foodVO : voList) {
            foodVO.setImgUrl(restaurantMapper.imgUrl(foodVO.getId()));
        }
        return voList;
    }
}
